package com.example.demo.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileUtil {

    public static void convertToGBKWithMeta(String filePath) {
        try {
            File file = new File(filePath);
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            StringBuilder content = new StringBuilder();
            int lineNumber = 0;
            while ((line = reader.readLine()) != null) {
                if (lineNumber == 1) {
                    content.append("<meta http-equiv=\"Content-Type\" content=\"text/html\"  charset=\"utf-8\">\n");
                }
                content.append(line).append("\n");
                lineNumber++;
            }
            reader.close();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
            writer.write(String.valueOf(content));
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
